<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class members extends Admin_Controller
{
	function lists()
	{
		global $CI;
		
		// model
		$CI->load->module_model('members.members_list_model');
		// sql
		$q = $CI->db->get(MEMBERS);
		
		// pagenavigation
		$CI->load->library('pagination');
        $config['base_url'] = site_url($CI->members_list_model->url_prefix.'/%s');
        $config['total_rows'] = $q->num_rows();
        $config['per_page'] = $CI->config->item('rows_per_page');
		$config['cur_page'] = $CI->members_list_model->start;
		$config['num_links'] = $CI->config->item('num_links');		
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
		
		// sql
		$CI->db->select('*');
		$CI->db->from(MEMBERS);				    	
		foreach ($CI->members_list_model->columns as $k=>$v)
		{
			if ($CI->members_list_model->cache->load($k))
				$CI->db->order_by($v, $CI->members_list_model->cache->load($k));
		}
		$CI->db->limit($CI->config->item('rows_per_page'), $CI->members_list_model->start);
		$q = $CI->db->get();		
		
		
		//Get image path
		$CI->load->library("fckeditor");	
		$path = $CI->fckeditor->UploadPath.'/images/';
		$this->add('image_franchise_path', $path);
		$items = $q->result();				
		foreach($items as &$item){
			$item->image_path = $path.$item->{MEMBERS_IMAGE};
		}
		// gridview
		$this->add('items', $items);
		$this->add('start', $CI->members_list_model->start);
		foreach ($CI->members_list_model->columns as $k=>$v)
		{
			if ($CI->members_list_model->cache->load($k))
				$this->add('sort_'.$k,  'class="sort_column_'.$CI->members_list_model->cache->load($k).'"');
		}

		// message
		$this->add('msg', $CI->members_list_model->cache->load($CI->members_list_model->msg_cache_prefix));
		$CI->members_list_model->cache->add($CI->members_list_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar('members.toolbar', array('parent_id'=>0, 'start'=>$CI->members_list_model->start));
		$this->module_navigation('Quản lý thành viên ');
		
		// view
		$this->view('members_list_view');
	}
	
	function detail()
	{
		global $CI;
		
		// model
		$CI->load->module_model('members.members_detail_model');
				
		$q = $CI->db->get_where(MEMBERS, array(MEMBERS_MEMBER_ID=>$CI->members_detail_model->item));
		$members = $q->row();
		
			
		// error
		$post = $CI->members_detail_model->cache->load('post');
		$CI->members_detail_model->cache->add('post', '');

		// add to view
		$this->add('fullname', $post?$post['fullname']:($members?$members->{MEMBERS_FULL_NAME}:''));
		$this->add('login_email', $post?$post['login_email']:($members?$members->{MEMBERS_LOGIN_EMAIL}:''));
		$this->add('password', $post?$post['password']:($members?$members->{MEMBERS_PW}:''));
		$this->add('address', $post?$post['address']:($members?$members->{MEMBERS_ADDRESS}:''));
		//$this->add('infomation', $post?$post['infomation']:($members?$members->{MEMBERS_INFO}:''));
		$this->add('type', $post?$post['type']:($members?$members->{MEMBERS_TYPE}:''));
		$this->add('qualification', $post?$post['qualification']:($members?$members->{MEMBERS_QUALIFICATION}:''));
		$this->add('active', $post?(isset($post['active'])?' checked':''):($members?($members->{MEMBERS_STATUS}?' checked':''):' checked'));
		$this->add('imagepath', ($members?$members->{MEMBERS_IMAGE}:''));		
		$this->add('start', $CI->members_detail_model->start);
		$this->add('item', $CI->members_detail_model->item);
		
				
		// editor
		$CI->load->library('fckeditor');
		$CI->fckeditor->InstanceName = 'infomation';
		$CI->fckeditor->Value = $post?$post['infomation']:($members?$members->{MEMBERS_INFO}:'');
		$CI->fckeditor->SetPathOfFranchise();
		$this->add('infomation', $CI->fckeditor->CreateHtml());
		
		// message
		$this->add('msg', $CI->members_detail_model->cache->load($CI->members_detail_model->msg_cache_prefix));
		$CI->members_detail_model->cache->add($CI->members_detail_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar();
		if ($CI->members_detail_model->item)
			$this->module_navigation('Quản lý thành viên : HIệu chỉnh');
		else
			$this->module_navigation('Quản lý thành viên : Thêm mới');
			
		// view
		$this->view('members_detail_view');
	}
	
}

?>